﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared;
using Sheego.Service.X400.Job.Shared.BO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl
{
    class X400MessageCache : IX400MessageCache
    {
        private HashSet<String> _cache = null;

        private HashSet<String> Cache
        {
            get
            {
                if (_cache == null)
                {
                    _cache = new HashSet<string>();
                    foreach (var id in FileManager.ReadCacheFile())
                        _cache.Add(id);
                }
                return _cache;
            }
        }

        IFileManager FileManager;
        public X400MessageCache(IFileManager fileManager)
        {
            FileManager = fileManager;
        }

        #region IX400MessageCache Members

        public bool Contains(IX400Message Message)
        {
            if (Message == null)
                throw new ArgumentNullException("Message");
            if (String.IsNullOrWhiteSpace(Message.Id))
                throw new ArgumentNullException("Message.Id");



            return Cache.Contains(Message.Id);

        }

        public void Add(IX400Message Message)
        {
            if (Message == null)
                throw new ArgumentNullException("Message");
            if (String.IsNullOrWhiteSpace(Message.Id))
                throw new ArgumentNullException("Message.Id");

            Cache.Add(Message.Id);
            FileManager.AppendCacheFile(Message.Id);
        }

        #endregion
    }
}
